/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.ResponsibilitiesLibBean;
import com.koron.purpose.service.api.ResponsibilitiesLibService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "responsibilitiesLib", description = "职责库")
@RestController
@RequestMapping("/responsibilitiesLib")
public class ResponsibilitiesLibController {

    private final static  Logger logger = LoggerFactory.getLogger(SafetyPualificationController.class);

    @Autowired
    private ResponsibilitiesLibService responsibilitiesLibService;

    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "item", value = "职责项",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "岗位名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "公司编码",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态(0-废弃 1-正常)",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryForList.htm", method = RequestMethod.GET)
    public MessageBean queryForList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@ApiIgnore String item,@ApiIgnore String keyword, @ApiIgnore String corpCode, @ApiIgnore String status,
                                    @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.queryForList(item,keyword, corpCode, status, page, pageSize);
    }

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/queryById.htm", method = RequestMethod.GET)
    public MessageBean queryById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.queryById(id);
    }

    @ApiOperation("停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteById.htm", method = RequestMethod.POST)
    @Log(title = "根据ID停用职责库", businessModule = "职责库", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean deleteById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.deleteById(id);
    }

    @ApiOperation("启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/start.htm", method = RequestMethod.POST)
    @Log(title = "根据ID启用职责库", businessModule = "职责库", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean start(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@ApiIgnore String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.start(id);
    }

    @ApiOperation("新增职责库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "item", value = "职责项", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "coding", value = "编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "排序号", required = true, paramType = "query", dataType = "int"),
            /*@ApiImplicitParam(name = "status", value = "状态(0-废弃 1-正常)", required = true, paramType = "query", dataType = "String"),*/
    })
    @RequestMapping(value = "/insert.htm", method = RequestMethod.POST)
    @Log(title = "新增职责库", businessModule = "职责库", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                              @ApiIgnore ResponsibilitiesLibBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.insert(user, bean);
    }

    @ApiOperation("修改职责库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "item", value = "职责项", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "coding", value = "编码", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "排序号", required = true, paramType = "query", dataType = "int"),
            /*@ApiImplicitParam(name = "status", value = "状态(0-废弃 1-正常)", required = true, paramType = "query", dataType = "String"),*/
    })
    @RequestMapping(value = "/update.htm", method = RequestMethod.POST)
    @Log(title = "修改职责库", businessModule = "职责库", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                              @ApiIgnore ResponsibilitiesLibBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.update(user, bean);
    }
    @ApiOperation("导入职责库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传的文件", required = true, paramType = "query", dataType = "String"),
            /*@ApiImplicitParam(name = "status", value = "状态(0-废弃 1-正常)", required = true, paramType = "query", dataType = "String"),*/
    })
    @RequestMapping(value = "/importExcel.htm", method = RequestMethod.POST)
    @Log(title = "导入职责库", businessModule = "职责库", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<String> importExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,MultipartFile file){
        if (null == user) {
            MessageBean<String> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return responsibilitiesLibService.importExcel(user, file);
    }

    @ApiOperation("下载导入职责库模板")
    @RequestMapping(value = "/exportTemplate.htm", method = RequestMethod.GET)
    public HttpEntity<?> exportTemplate(){
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/responsibilitiesLibTemplate.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/responsibilitiesLibTemplate.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("职责库模板", targetFile, "default.btl", map);
    }
}
